<?php
/**
 * Description: 对接eeo api
 * User: devforma
 * Date: 16/3/14
 * Time: 11:56
 */

class EEO {

    const baseUrl = 'http://www.eeo.cn/partner/api/course.api.php?action=';
    const defaultPassword = '123456';
    const defaultStudentPassword = 'yaoguo123';

    //机构相关标识
    private static $PARTNER_SID = '';
    private static $PARTNER_SECRET = '';

    /**
     * 创建课程
     *
     * @param int $courseName 课程名
     * @param int $folderId 资源id
     * @return bool
     */
    public function addCourse($courseName, $folderId = 0) {
        $url = self::baseUrl . 'addCourse';

        $params = self::genParams();
        $params['courseName'] = $courseName;

        if ($folderId !== 0) {
            $params['folderId'] = $folderId;
        }

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return $result->data; //返回eeo课程id
        }

        return false;
    }


    /**
     * 编辑课程
     *
     * @param int $eeoCourseId eeo课程id
     * @param string $courseName 课程名
     * @param int $folderId 资源id
     * @return bool
     */
    public function editCourse($eeoCourseId, $courseName = '', $folderId = 0) {
        $url = self::baseUrl . 'editCourse';

        $params = self::genParams();
        $params['courseName'] = $courseName;
        $params['courseId'] = $eeoCourseId;

        if ($courseName !== '') {
            $params['courseName'] = $courseName;
        }

        if ($folderId !== 0) {
            $params['folderId'] = $folderId;
        }

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return true;
        }

        return false;
    }

    /**
     * 删除课程
     *
     * @param int $eeoCourseId eeo课程id
     * @return bool
     */
    public function deleteCourse($eeoCourseId) {
        $url = self::baseUrl . 'delCourse';

        $params = self::genParams();
        $params['courseId'] = $eeoCourseId;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return true;
        }

        return false;
    }


    /**
     * 创建eeo课堂
     *
     * @param int $eeoCourseId eeo课程id
     * @param string $className 课堂名称
     * @param int $beginTime 开始时间戳
     * @param int $endTime 结束时间戳
     * @param string $teacherPhone 教师账号(手机号)
     * @param string $teacherName 教师名称
     * @return bool|int
     */
    public function addClass($eeoCourseId, $className, $beginTime, $endTime, $teacherPhone, $teacherName) {
        $url = self::baseUrl . 'addCourseClass';

        $params = self::genParams();
        $params['courseId'] = $eeoCourseId;
        $params['className'] = $className;
        $params['beginTime'] = $beginTime;
        $params['endTime'] = $endTime;
        $params['teacherAccount'] = $teacherPhone;
        $params['teacherName'] = $teacherName;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return $result->data;
        }

        return false;
    }


    /**
     * 修改eeo课堂
     *
     * @param int $eeoCourseId eeo课程id
     * @param int $eeoClassId eeo课堂id
     * @param string $className 课堂名称
     * @param int $beginTime 开始时间戳
     * @param int $endTime 结束时间戳
     * @param string $teacherPhone 教师账号(手机号)
     * @param string $teacherName 教师名称
     * @return bool|int
     */
    public function editClass($eeoCourseId, $eeoClassId, $className = '', $beginTime = 0, $endTime = 0, $teacherPhone = '', $teacherName = '') {
        $url = self::baseUrl . 'editCourseClass';

        $params = self::genParams();
        $params['courseId'] = $eeoCourseId;
        $params['classId'] = $eeoClassId;

        if ($className !== '') {
            $params['className'] = $className;
        }

        if ($beginTime !== 0) {
            $params['beginTime'] = $beginTime;
        }

        if ($endTime !== 0) {
            $params['endTime'] = $endTime;
        }

        if ($teacherPhone !== '') {
            $params['teacherAccount'] = $teacherPhone;
        }

        if ($teacherName !== '') {
            $params['teacherName'] = $teacherName;
        }

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return true;
        }

        return false;
    }


    /**
     * 获取课程下的课堂列表
     *
     * @param int $eeoCourseId eeo课程id
     * @return bool
     */
    public function getCourseClass($eeoCourseId) {
        $url = self::baseUrl . 'getCourseClass';

        $params = self::genParams();
        $params['courseId'] = $eeoCourseId;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return $result->data;
        }

        return false;
    }


    /**
     * 删除eeo课堂
     *
     * @param int $eeoCourseId eeo课程id
     * @param int $eeoClassId eeo课堂id
     * @return bool
     */
    public function deleteClass($eeoCourseId, $eeoClassId) {
        $url = self::baseUrl . 'delCourseClass';

        $params = self::genParams();
        $params['courseId'] = $eeoCourseId;
        $params['classId'] = $eeoClassId;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return true;
        }

        return false;
    }


    /**
     * 为用户开通eeo课程
     *
     * @param int $eeoCourseId eeo课程id
     * @param string $studentPhone 学生手机号
     * @param string $studentName 学生名称
     * @param bool $isObserver 是否为旁听身份
     * @return bool
     */
    public function addCourseStudent($eeoCourseId, $studentPhone, $studentName, $isObserver = false) {
        if (!$this->registerUser($studentPhone, self::defaultStudentPassword)) {
            return false;
        }

        $url = self::baseUrl . 'addCourseStudent';

        $params = self::genParams();
        $params['courseId'] = $eeoCourseId;
        $params['studentAccount'] = $studentPhone;
        $params['studentName'] = $studentName;
        $params['identity'] = $isObserver ? 2 : 1;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return true;
        }

        return false;
    }


    /**
     * 获取课程下的学生列表
     *
     * @param $eeoCourseId
     * @param bool $isObserver
     * @return bool
     */
    public function getCourseStudents($eeoCourseId, $isObserver = false) {
        $url = self::baseUrl . 'getCourseStudent';

        $params = self::genParams();
        $params['courseId'] = $eeoCourseId;
        $params['identity'] = $isObserver ? 2 : 1;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return $result->data;
        }

        return false;
    }


    /**
     * 从课程里删除学生
     *
     * @param int $eeoCourseId eeo课程id
     * @param string $studentPhone 学生手机号
     * @param bool $isObserver 是否为旁听
     * @return bool
     */
    public function deleteCourseStudent($eeoCourseId, $studentPhone, $isObserver = false) {
        $url = self::baseUrl . 'delCourseStudent';

        $params = self::genParams();
        $params['courseId'] = $eeoCourseId;
        $params['studentAccount'] = $studentPhone;
        $params['identity'] = $isObserver ? 2 : 1;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return true;
        }

        return false;
    }


    /**
     * eeo系统注册用户
     *
     * @param string $studentPhone 手机号
     * @param string $password 明文密码
     * @return bool
     */
    public function registerUser($studentPhone, $password) {
        $url = self::baseUrl . 'register';

        $params = self::genParams();
        $params['telephone'] = $studentPhone;
        $params['password'] = $password;

        if (($result = self::httpPost($url, $params)) && $result->data) {
            return $result->data;
        }

        return false;
    }


    /**
     * eeo系统注册教师
     *
     * @param $teacherPhone
     * @param $teacherName
     * @return bool
     */
    public function registerTeacher($teacherPhone, $teacherName) {
        if (!$this->registerUser($teacherPhone, self::defaultPassword)) {
            return false;
        }
        
        $url = self::baseUrl . 'addTeacher';

        $params = self::genParams();
        $params['teacherAccount'] = $teacherPhone;
        $params['teacherName'] = $teacherName;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return $result->data;
        }

        return false;
    }


    /**
     * eeo教师编辑
     *
     * @param $eeoTeacherId
     * @param $teacherName
     * @return bool
     */
    public function editTeacher($eeoTeacherId, $teacherName) {
        $url = self::baseUrl . 'editTeacher';

        $params = self::genParams();
        $params['teacherName'] = $teacherName;
        $params['st_id'] = $eeoTeacherId;

        if (($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
            return true;
        }

        return false;
    }


    /**
     * 获取eeo课堂地址
     *
     * @param $eeoCourseId
     * @param $eeoClassId
     * @param $studentPhone
     * @return bool|string
     */
    public function getClassEntryUrl($eeoCourseId, $eeoClassId, $studentPhone) {
        $url = self::baseUrl . 'getTempLoginKey';

        $params = self::genParams();
        $params['telephone'] = $studentPhone;
        $params['lifeTime'] = 10*60; //10分钟有效期

        //获取clientId
        $classes = $this->getCourseClass($eeoCourseId);
        $clientCourseId = $clientClassId = 0;
        if (!empty($classes) && is_array($classes)) {
            foreach ($classes as $class) {
                if ($class->class_id == $eeoClassId) {
                    $clientCourseId = $class->client_course_id;
                    $clientClassId = $class->client_class_id;
                    break;
                }
            }

            if (!empty($clientCourseId) && !empty($clientClassId) && ($result = self::httpPost($url, $params)) && $result->error_info->errno == 1) {
                $url = sprintf('classin://www.eeo.cn/enterclass?telephone=%s&authTicket=%s&classId=%s&courseId=%s&schoolId=%s',
                    $studentPhone, $result->data, $clientClassId, $clientCourseId, $params['SID']);
                return $url;
            }
        }

        return false;
    }


    /**
     * 生成接口请求参数
     *
     * @return array
     */
    private static function genParams() {
        $now = time();
        return array(
            'SID' => self::$PARTNER_SID,
            'timeStamp' => $now,
            'safeKey' => md5(self::$PARTNER_SECRET . $now)
        );
    }

    /**
     * POST访问
     *
     * @param $url
     * @param $data
     * @return mixed
     */
    private static function httpPost($url, $data) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $data = curl_exec($ch);
        curl_close($ch);

        if ($data)
            return json_decode($data);

        return false;
    }
}